package com.cn.ZhiKeDianShang.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cn.ZhiKeDianShang.AppConfig;
import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.base.BaseActivity;
import com.cn.ZhiKeDianShang.model.response.UserInfoBean;
import com.cn.ZhiKeDianShang.presenter.LoginUserPresenter;
import com.cn.ZhiKeDianShang.presenter.view.LoginUserView;
import com.cn.ZhiKeDianShang.util.ToastUtil;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.HttpUtils;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.ObjectResult;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.Result;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/7/3.
 * do what to 2017/7/3
 */

public class UpdatePhoneActivity extends BaseActivity<LoginUserView, LoginUserPresenter> implements LoginUserView {


    @BindView(R.id.tv_sendcode)
    TextView tvSendcode;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;


    private int reckonTime = 60;
    private Handler mReckonHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0x1) {
                //倒计时  控件背景颜色
                if (tvSendcode != null) {
                    tvSendcode.setText(reckonTime + " " + "S");
                }
                reckonTime--;
                if (reckonTime < 0) {
                    mReckonHandler.sendEmptyMessage(0x2);
                } else {
                    mReckonHandler.sendEmptyMessageDelayed(0x1, 1000);
                }
            } else if (msg.what == 0x2) {
                // 60秒结束
                if (tvSendcode != null) {
                    tvSendcode.setEnabled(true);
                    tvSendcode.setText("获取验证码");
                }
                //控件 没点击背景颜色
//                tvSendcode.setBackgroundResource(R.drawable.ql_l_g_bg_ripple);
                reckonTime = 60;
            }
        }
    };

    @Override
    protected int getNormalLayoutId() {
        return R.layout.activity_update_phone;
    }

    @Override
    protected int getErrorLayoutId() {
        return R.layout.activity_update_phone;
    }

    @Override
    protected LoginUserPresenter createPresenter() {
        return new LoginUserPresenter(this, this);
    }

    @Override
    protected void initViewsAndEvents() {
    }


    /**
     * 获得验证码 接口
     */
    private void smsSend() {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        params.put("mobile", etPhone.getText().toString());
        params.put("event", "3");
        HttpUtils.post().url(AppConfig.SMS_SEND)
                .params(params)
                .build()
                .execute(new BaseCallback<LoginRegisterResult>(LoginRegisterResult.class) {
                    @Override
                    public void onResponse(ObjectResult<LoginRegisterResult> result) {
//                        DialogHelper.dismissProgressDialog();
                        Log.d("Debug", "到达请求这里---");
                        if (Result.checkSuccess(UpdatePhoneActivity.this, result)) {
                            ToastUtil.showShort("发送成功 注意查收");
                            tvSendcode.setEnabled(false);
                            //倒计时  控件背景颜色
//                tvSendcode.setBackgroundResource(R.drawable.bg_verify_sure_grey);
                            // 开始倒计时
                            mReckonHandler.sendEmptyMessage(0x1);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                    }
                });
    }


    /**
     * 修改手机号
     */
    private void updatePhone() {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        params.put("mobile", etPhone.getText().toString());
        params.put("captcha", etCode.getText().toString());
        HttpUtils.post().url(AppConfig.USER_CHANGEMOBILE)
                .params(params)
                .addSecret()
                .build()
                .execute(new BaseCallback<LoginRegisterResult>(LoginRegisterResult.class) {
                    @Override
                    public void onResponse(ObjectResult<LoginRegisterResult> result) {
//                        DialogHelper.dismissProgressDialog();
                        Log.d("Debug", "到达请求这里---");
                        if (Result.checkSuccess(UpdatePhoneActivity.this, result)) {
                            ToastUtil.showShort("修改成功");
                            finish();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                    }
                });
    }


    @Override
    public void onLogin(UserInfoBean resultBean) {

    }

    @Override
    public void requestFailed(String message) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.setting_back, R.id.tv_sendcode, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.setting_back:
                finish();
                break;
            case R.id.tv_sendcode:
                if (etPhone.getText().toString().length() == 0) {
                    ToastUtil.showShort("请输入手机号码");
                    return;
                }
                smsSend();


                break;
            case R.id.tv_submit:
                if (etPhone.getText().toString().length() == 0) {
                    ToastUtil.showShort("请输入手机号码");
                    return;
                }
                if (etCode.getText().toString().length() == 0) {
                    ToastUtil.showShort("请输入短信验证码");
                    return;
                }
                updatePhone();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mReckonHandler.removeCallbacksAndMessages(null);
    }
}
